package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbCwsfMapper;
import com.it.tydic.estate.module.model.inner.TbCwsf;
import com.it.tydic.estate.module.service.TbCwsfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:   车位收费管理ServiceImpl
 * @author: zhangshuo
 * @createDate: 2020/03/02
 * @version: 1.0
 */
@Service
public class TbCwsfServiceImpl implements TbCwsfService {
    @Autowired
    private TbCwsfMapper tbCwsfMapper;

    /*车位收费条件查询*/
    @Override
    public Result selectBysfPage(Map<String,Object> tbCwsf) {
        Map<String,Object> map = new HashMap<>();
        if(tbCwsf.containsKey("pageNum") && tbCwsf.containsKey("pageSize")){//判断是否有分页条件有进行分页
            PageHelper.startPage((Integer) tbCwsf.get("pageNum"), (Integer) tbCwsf.get("pageSize"));
        }
        Page<TbCwsf> cheweishoufei = tbCwsfMapper.selectBysfPage(tbCwsf);
        /*转换字典组名称*/
        for (TbCwsf cwsf : cheweishoufei) {
            /*计费周期单位*/
            cwsf.setCwscJfzqdw(DictionaryCache.getDicVal(cwsf.getCwscJfzqdw()));
            /*计费周期数*/
            cwsf.setCwscJfzqs(DictionaryCache.getDicVal(cwsf.getCwscJfzqs()));
            /*收费周期数*/
            cwsf.setCwscSfzqs(DictionaryCache.getDicVal(cwsf.getCwscSfzqs()));
            /*是否收费*/
            cwsf.setCwscSfsf(DictionaryCache.getDicVal(cwsf.getCwscSfsf()));
        }
        List<TbCwsf> result = cheweishoufei.getResult();
        map.put("result",result);
        map.put("totle",cheweishoufei.getTotal());
        return   Result.success(map);
    }
    /*车位收费批量删除*/
    @Override
    public Result deleteByTbCwsf(List<String> list) {
        if(list==null || list.size()<=0){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        int result = tbCwsfMapper.deleteByTbCwsf(list);
        if(result<=0){
            return   Result.error(CodeMsg.DELETE_ERROR);
        }
        return Result.success();
    }

    /*车位收费新增或修改*/
    @Override
    public Result insertTbCwsfOrUpdata(TbCwsf tbCwsf)  {
        long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
        if(tbCwsf.getcwscId() == null || "".equals(tbCwsf.getcwscId())) {
            tbCwsf.setcwscId(String.valueOf(id));
            tbCwsf.setCwscZfbj("0");
            tbCwsf.setcwscCjsj(new Date());
            tbCwsf.setcwscXgsj(new Date());
            int result = tbCwsfMapper.insertSelective(tbCwsf);
            if (result < 0)
                return Result.error(CodeMsg.INSERT_ERROR);
        }else{
            tbCwsf.setcwscXgsj(new Date());
            int result = tbCwsfMapper.updateByPrimaryKeySelective(tbCwsf);
        }
        return Result.success(tbCwsf);

    }
    /*根据车位ID查询车位收费信息*/
    @Override
    public Result selectCwsfBycwid(List<String> list) {

        List<TbCwsf>shoufei = tbCwsfMapper.selectCwsfBycwid(list);

        return   Result.success(shoufei);
    }

}
